package com.example.addressdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.addressdemo.DB.DBAdapter;

public class DetailActivity extends AppCompatActivity {
    private PopupWindow popupwindow;
    private DBAdapter dbAdapter;
    private ImageView det_head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("DetailActivity","onCreate") ;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        TextView det_name = (TextView) findViewById(R.id.det_name);
        TextView det_phone = (TextView) findViewById(R.id.det_phone);
        TextView det_work = (TextView) findViewById(R.id.det_work);
        TextView det_home = (TextView) findViewById(R.id.det_home);
        ImageButton left = (ImageButton) findViewById(R.id.det_left);
        ImageButton setting = (ImageButton) findViewById(R.id.det_set);
        ImageButton call=(ImageButton) findViewById(R.id.det_call);
        det_head=(ImageView) findViewById(R.id.det_head);


        dbAdapter = new DBAdapter(this);
        final Bundle bundle = this.getIntent().getExtras();
        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        String work = bundle.getString("work");
        String home = bundle.getString("home");

        det_name.setText(name);
        det_phone.setText(phone);
        det_work.setText(work);
        det_home.setText(home);

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupwindow != null && popupwindow.isShowing()) {
                    Log.d("setting ","dismiss");
                    popupwindow.dismiss();
                    return;
                } else {
                    Log.d("setting ","init");
                    initmPopupWindowView(bundle);
                    popupwindow.showAsDropDown(view, -300, 0);
                }
            }
        });

        call.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(DetailActivity.this, CallActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    public void initmPopupWindowView(final Bundle bundle) {

        View customView = getLayoutInflater().inflate(R.layout.popview_item, null, false);// 获取自定义布局文件pop.xml的视图
        popupwindow = new PopupWindow(customView, 400, 400);   // 创建PopupWindow实例,200,150分别是宽度和高度
        // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
        //popupwindow.setAnimationStyle(R.style.AnimationFade);
        // 自定义view添加触摸事件
        customView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupwindow != null && popupwindow.isShowing()) {
                    popupwindow.dismiss();
                    popupwindow = null;
                }

                return false;
            }
        });
        /** 在这里可以实现自定义视图的功能 */
        Button update_contact = (Button) customView.findViewById(R.id.update_contact);
        Button delete_contact = (Button) customView.findViewById(R.id.delete_contact);

        delete_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("delete_contact onClick",bundle.getString("name"));
                long result=dbAdapter.delete(bundle.getString("name"));
                System.out.println(result);
                Toast.makeText(getApplicationContext(), "Delete successed", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(DetailActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        update_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("update_contact onClick",bundle.getString("name"));
                bundle.putString("type", "update");
                Intent intent = new Intent(DetailActivity.this, AddActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}


